package com.client;

import java.util.ArrayList;
import java.util.List;

import com.client.controller.AnneeTextBoxController;
import com.client.controller.EntryController;
import com.client.controller.IfThenCheckController;
import com.client.controller.JourTextBoxController;
import com.client.controller.MoisTextBoxController;
import com.client.controller.NotNullTextBoxController;
import com.client.controller.NumericController;
import com.client.service.UserManagementService;
import com.client.service.UserManagementServiceAsync;
import com.client.service.data.AssureDataService;
import com.client.service.data.AssureDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.shared.context.User;
import com.shared.model.Assure;
import com.shared.utilitaire.Adresse;
import com.shared.utilitaire.Date;





public class Inscription {

        private TextBox boxNom = new TextBox();
        private TextBox boxPrenom = new TextBox();
        private TextBox boxJour = new TextBox();
        private TextBox boxMois = new TextBox();
        private TextBox boxAnnee = new TextBox();
        private TextBox boxJourPermis = new TextBox();
        private TextBox boxMoisPermis = new TextBox();
        private TextBox boxAnneePermis = new TextBox();
        private RadioButton radioOui = new RadioButton("groupeOuiNon", "Oui");
        private RadioButton radioNon = new RadioButton("groupeOuiNon", "Non");
        private TextBox boxRecupJour = new TextBox();
        private TextBox boxRecupMois = new TextBox();
        private TextBox boxRecupAnnee = new TextBox();
        private TextBox boxMail = new TextBox();
        private TextBox boxTel = new TextBox();
        private TextBox boxMobile = new TextBox();
        private ListBox comboCivilite = new ListBox();
        
        RadioButton radioParticulier = new RadioButton("groupeProPart", "Particulier");
        RadioButton radioPro = new RadioButton("groupeProPart", "Professionnel");
        private boolean isRecup = false;

        private TextBox boxLogin = new TextBox();
        private PasswordTextBox boxPassword = new PasswordTextBox();

        private List<EntryController> listEntryControllers = new ArrayList<EntryController>();
        IfThenCheckController jourRecupController = new IfThenCheckController(new JourTextBoxController(boxRecupJour, "Jour de reobtention"), isRecup);
        IfThenCheckController moisRecupController = new IfThenCheckController(new MoisTextBoxController(boxRecupMois, "Mois de reobtention"), isRecup);
        IfThenCheckController anneeRecupController = new IfThenCheckController(new AnneeTextBoxController(boxRecupAnnee, "Annee de reobtention"), isRecup);
        
        private final UserManagementServiceAsync userService = GWT.create(UserManagementService.class);
       
       
        public Inscription() {
        	listEntryControllers.add(jourRecupController);
        	listEntryControllers.add(moisRecupController);
        	listEntryControllers.add(anneeRecupController);
        }
               
       
       
        @SuppressWarnings("deprecation")
        public AbsolutePanel createInscription(){
               
                AbsolutePanel panelInscription = new AbsolutePanel();
               
                //mainPanel.add(panelInscription, 23, 24);
                panelInscription.setSize("674px", "600px");
               
                Label lblInscription = new Label("Inscription :");
                lblInscription.setStyleName("titre") ;
                panelInscription.add(lblInscription, 6, 6);

               
                Label labelCivilite = new Label("Civilite :");
                labelCivilite.addStyleName("gras");
                panelInscription.add(labelCivilite, 6, 56);
               
               
               
                List<String> listeCivilite = new ArrayList<String>() ;
                listeCivilite.add("Mr");
                listeCivilite.add("Mme") ;
                listeCivilite.add("Melle");
                for(String s: listeCivilite){
                        comboCivilite.addItem(s) ;
                }
                panelInscription.add(comboCivilite, 95, 50);
               
                Label labelNom = new Label("Nom :");
                labelNom.addStyleName("gras");
                panelInscription.add(labelNom, 6, 87);
               
                Label labelPrenom = new Label("Prenom :");
                labelPrenom.addStyleName("gras");
                panelInscription.add(labelPrenom, 6, 119);
               
               
                panelInscription.add(boxNom, 95, 86);
                boxNom.setWidth("179px");
               
               
                panelInscription.add(boxPrenom, 95, 119);
                boxPrenom.setWidth("179px");
               
                Label labelNaissance = new Label("Date de naissance :");
                labelNaissance.addStyleName("gras");
                panelInscription.add(labelNaissance, 6, 154);
                             
                panelInscription.add(boxJour, 167, 154);
                boxJour.setWidth("27px");
                boxJour.setMaxLength(2);
                
                                
                
                Label slash = new Label("/");
                panelInscription.add(slash, 200, 154);
                slash.setSize("14px", "21px");
               
               
                panelInscription.add(boxMois, 210, 154);
                boxMois.setWidth("27px");
                boxMois.setMaxLength(2);
                
                Label slash2 = new Label("/");
                panelInscription.add(slash2, 243, 154);
                slash2.setSize("14px", "21px");
               
               
                panelInscription.add(boxAnnee, 253, 154);
                boxAnnee.setWidth("47px");
                boxAnnee.setMaxLength(4);
                
                
                Label labelDatePermis = new Label("Date d'obtention du permis :");
                labelDatePermis.addStyleName("gras");
                panelInscription.add(labelDatePermis, 6, 181);
               
               
                panelInscription.add(boxJourPermis, 220, 182);
                boxJourPermis.setWidth("27px");
               
                Label slash3 = new Label("/");
                panelInscription.add(slash3, 253, 182);
                slash3.setSize("14px", "21px");
               
               
                panelInscription.add(boxMoisPermis, 263, 182);
                boxMoisPermis.setWidth("27px");
               
                Label slash4 = new Label("/");
                panelInscription.add(slash4, 296, 182);
                slash4.setSize("14px", "21px");
               
               
                panelInscription.add(boxAnneePermis, 306, 182);
                boxAnneePermis.setWidth("47px");
               
                Label labelValide = new Label("Est-il valide ?");
                labelValide.addStyleName("gras");
                panelInscription.add(labelValide, 6, 208);
               
               
                panelInscription.add(radioOui, 108, 208);
               
               
                panelInscription.add(radioNon, 167, 208);
                radioNon.setSize("44px", "21px");
               
              
                Label labelRecup = new Label("Si non, quand le recuperez-vous ?");
                labelRecup.addStyleName("gras");
                 radioOui.setValue(true);
                 boxRecupAnnee.setEnabled(false);
             	boxRecupMois.setEnabled(false);
             	boxRecupJour.setEnabled(false);	
                
                radioNon.addClickHandler(new ClickHandler() {
				
                
					
					public void onClick(ClickEvent event) {
						
						boxRecupAnnee.setEnabled(true);
	                	boxRecupMois.setEnabled(true);
	                	boxRecupJour.setEnabled(true);
	                	isRecup=true;
	                	jourRecupController.setCondition(true);
	                	moisRecupController.setCondition(true);
	                	anneeRecupController.setCondition(true);
					}
				});
                
                radioOui.addClickHandler(new ClickHandler() {
					
					
					public void onClick(ClickEvent event) {
						
						boxRecupAnnee.setEnabled(false);
	                	boxRecupMois.setEnabled(false);
	                	boxRecupJour.setEnabled(false);	
	                	isRecup = false ;
	                	jourRecupController.setCondition(false);
	                	moisRecupController.setCondition(false);
	                	anneeRecupController.setCondition(false);
					}
				});
                
                
                
                panelInscription.add(labelRecup, 6, 232);
               
               
                panelInscription.add(boxRecupJour, 253, 230);
                boxRecupJour.setWidth("27px");
               
                Label slash5 = new Label("/");
                panelInscription.add(slash5, 286, 230);
                slash5.setSize("14px", "21px");
               
               
                panelInscription.add(boxRecupMois, 296, 230);
                boxRecupMois.setWidth("27px");
               
                Label slash6 = new Label("/");
                panelInscription.add(slash6, 329, 230);
                slash6.setSize("14px", "21px");
               
               
                panelInscription.add(boxRecupAnnee, 339, 230);
                boxRecupAnnee.setWidth("47px");
               
               
                //Adresse
                final AbsolutePanel panelAdresse = new AbsolutePanel();
                panelInscription.add(panelAdresse, 6, 265);
                panelAdresse.setSize("412px", "400px");
                final PanelAdresse panAdr = new PanelAdresse("Adresse :");
                final PanelAdresse panAdr2 = new PanelAdresse("Adresse supplementaire :");
                panelAdresse.add(panAdr);
               
                final Hyperlink linkAdresse2 = new Hyperlink("Une autre adresse ?", "Adresse2");
                final Hyperlink linkAdresse1 = new Hyperlink("Retour","Etape precedente");
                linkAdresse1.setSize("150px", "21px");
                linkAdresse2.setSize("150px", "21px");
                panelAdresse.add(linkAdresse2, 256, 261);
                
                linkAdresse2.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    
                    panelAdresse.clear();                    
                    panelAdresse.add(panAdr2) ; 
                    panelAdresse.add(linkAdresse1, 256, 261);
                        }
                    });
                
                linkAdresse1.addClickHandler(new ClickHandler() {			
        			
        			public void onClick(ClickEvent event) {
        				
        				panelAdresse.clear();        	            
        	            panelAdresse.add(panAdr) ;
        	        	panelAdresse.add(linkAdresse2, 256, 261);
        				
        			}
        		});
               
                Label labelMail = new Label("Mail :");
                labelMail.addStyleName("gras");
             
                panelInscription.add(labelMail, 364, 56);
               
               
                panelInscription.add(boxMail, 406, 56);
                boxMail.setWidth("224px");
               
                Label labelTel = new Label("Telephone :");
                labelTel.addStyleName("gras");
                panelInscription.add(labelTel, 316, 99);
               
               
                panelInscription.add(boxTel, 406, 99);
               
                Label labelMobile = new Label("Mobile :");
                labelMobile.addStyleName("gras");
                panelInscription.add(labelMobile, 346, 136);
               
               
                panelInscription.add(boxMobile, 406, 135);
               

                Label labelVousEtes = new Label("Vous etes :");
                labelVousEtes.addStyleName("gras");
               
                panelInscription.add(labelVousEtes, 6, 29);

                Label labelLogin = new Label("Login :");
                labelLogin.addStyleName("gras");
                panelInscription.add(labelLogin, 406, 182);

               

                panelInscription.add(radioPro, 95, 29);

                panelInscription.add(boxLogin, 460, 182);

               
                Label labelPassword = new Label("Mot de passe :");
                labelPassword.addStyleName("gras");
                panelInscription.add(labelPassword, 406, 208);
               

                panelInscription.add(radioParticulier, 224, 29);
               radioPro.setValue(true);
               

                panelInscription.add(boxPassword, 510, 208);
               
                //Controllers
                listEntryControllers.add(new JourTextBoxController(boxJour,"NaissanceJour"));
                listEntryControllers.add(new JourTextBoxController(boxJourPermis,"PermisJour"));
                listEntryControllers.add(new MoisTextBoxController(boxMois, "NaissanceMois"));
                listEntryControllers.add(new MoisTextBoxController(boxMoisPermis, "PermisMois"));
                listEntryControllers.add(new AnneeTextBoxController(boxAnnee, "NaissanceAnnee"));
                listEntryControllers.add(new AnneeTextBoxController(boxAnneePermis, "PermisAnnee"));
                
                               
                listEntryControllers.add(new NotNullTextBoxController(boxPassword, "Password"));
                listEntryControllers.add(new NotNullTextBoxController(boxLogin, "Login"));
                listEntryControllers.add(new NotNullTextBoxController(boxNom, "Nom"));
                listEntryControllers.add(new NotNullTextBoxController(boxPrenom, "Prenom"));
                listEntryControllers.add(new NotNullTextBoxController(boxMail, "Mail"));
               
                
                Button boutonEnvoyer = new Button("New button");
                boutonEnvoyer.setText("Envoyer");
                boutonEnvoyer.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                        		String errorMsg ="";
                        		boolean correct = true;
                        		for(EntryController entryController : listEntryControllers){
                        			if(!entryController.check()){
                        				correct=false;
                        				errorMsg = errorMsg+"\n"+entryController.getSource();
                        			}
                        		}
                        		
                        		if(correct){
                        
                                Adresse adr = new Adresse();                                
                                
                                adr.setCodePostal(panAdr.getBoxCodePostal().getValue()) ;
                                adr.setNomVoie(panAdr.getBoxVoie().getValue()) ;
                                adr.setNumero(panAdr.getBoxNumero().getValue());
                                adr.setPays(panAdr.getBoxPays().getValue());
                                adr.setTypeVoie(panAdr.getComboBox().getValue(panAdr.getComboBox().getSelectedIndex()));
                                adr.setVille(panAdr.getBoxVille().getValue()) ;
                                
                                Adresse adr2 = new Adresse();                                
                                
                                adr2.setCodePostal(panAdr2.getBoxCodePostal().getValue()) ;
                                adr2.setNomVoie(panAdr2.getBoxVoie().getValue()) ;
                                adr2.setNumero(panAdr2.getBoxNumero().getValue());
                                adr2.setPays(panAdr2.getBoxPays().getValue());
                                adr2.setTypeVoie(panAdr2.getComboBox().getValue(panAdr.getComboBox().getSelectedIndex()));
                                adr2.setVille(panAdr2.getBoxVille().getValue()) ;
                                
                       
                                Assure ass1 = new Assure();
                                ass1.setAdresse1(adr);
                                ass1.setAdresse2(adr2);
                                ass1.setCivilite(getComboCivilite().getValue(getComboCivilite().getSelectedIndex()));
                                ass1.setDateNaissance(new Date(getBoxJour().getValue(),getBoxMois().getValue(),getBoxAnnee().getValue()));
                                ass1.setDatePermis(new Date(getBoxJourPermis().getValue(),getBoxMoisPermis().getValue(),getBoxAnneePermis().getValue()));
                                
                                if (isRecup) {
                                	Date dateRecup = new Date();
                                	dateRecup.setJour(Integer.valueOf(getBoxRecupJour().getValue()));
                                	dateRecup.setMois(Integer.valueOf(getBoxRecupMois().getValue()));
                                	dateRecup.setAnnee(Integer.valueOf(getBoxRecupAnnee().getValue()));
                              
                                	ass1.setDateReobtention(dateRecup);
                                	
                                }
                                
                                ass1.setMail(getBoxMail().getValue());
                                ass1.setNom(getBoxNom().getValue());
                                ass1.setNumeroMobile(getBoxMobile().getValue());
                                ass1.setNumeroTel(getBoxTel().getValue());
                                ass1.setPrenom(getBoxPrenom().getValue());
                                ass1.setPermisAnnule(isRecup);
                                ass1.setType(getValeurType());
                               

                               
                                //assureService.save(ass1, new AsyncCallback<Assure>() {

                                User user = new User();
                                user.setName(getBoxLogin().getValue());
                                user.setPassword(getBoxPassword().getValue());
                               
                                userService.save(user, ass1, new AsyncCallback<User>() {

   
                                        public void onSuccess(User result) {
                                                Window.confirm("Vous pouvez maintenant vous identifier");
                                        }
                                       
                                       
                                        public void onFailure(Throwable caught) {
                                                Window.alert(caught.getMessage());
                                               
                                        }
                                });
                        		}else{
                        			Window.alert(errorMsg);
                        		}
                    	
                        }

                });
                panelInscription.add(boutonEnvoyer, 500, 250);

                return panelInscription ;
       
        }


        /**
         * @return the boxNom
         */
        public TextBox getBoxNom() {
                return boxNom;
        }


        /**
         * @return the boxPrenom
         */
        public TextBox getBoxPrenom() {
                return boxPrenom;
        }


        /**
         * @return the boxJour
         */
        public TextBox getBoxJour() {
                return boxJour;
        }

        /**
         * @return the boxMois
         */
        public TextBox getBoxMois() {
                return boxMois;
        }


        /**
         * @return the boxAnnee
         */
        public TextBox getBoxAnnee() {
                return boxAnnee;
        }


        /**
         * @return the boxJourPermis
         */
        public TextBox getBoxJourPermis() {
                return boxJourPermis;
        }


        /**
         * @return the boxMoisPermis
         */
        public TextBox getBoxMoisPermis() {
                return boxMoisPermis;
        }


        /**
         * @return the boxAnneePermis
         */
        public TextBox getBoxAnneePermis() {
                return boxAnneePermis;
        }



        /**
         * @return the radioOui
         */
        public RadioButton getRadioOui() {
                return radioOui;
        }



        /**
         * @return the radioNon
         */
        public RadioButton getRadioNon() {
                return radioNon;
        }


        /**
         * @return the boxRecupJour
         */
        public TextBox getBoxRecupJour() {
                return boxRecupJour;
        }


        /**
         * @return the boxRecupMois
         */
        public TextBox getBoxRecupMois() {
                return boxRecupMois;
        }


        /**
         * @return the boxRecupAnnee
         */
        public TextBox getBoxRecupAnnee() {
                return boxRecupAnnee;
        }


        /**
         * @return the boxMail
         */
        public TextBox getBoxMail() {
                return boxMail;
        }



        /**
         * @return the boxTel
         */
        public TextBox getBoxTel() {
                return boxTel;
        }


        /**
         * @return the boxMobile
         */
        public TextBox getBoxMobile() {
                return boxMobile;
        }


       
        /**
         * @return the comboCivilite
         */
        public ListBox getComboCivilite() {
                return comboCivilite;
        }



        /**
         * @return the radioParticulier
         */
        public RadioButton getRadioParticulier() {
                return radioParticulier;
        }



        /**
         * @return the radioPro
         */
        public RadioButton getRadioPro() {
                return radioPro;
        }



        /**
         * @return the boxLogin
         */
        public TextBox getBoxLogin() {
                return boxLogin;
        }



        /**
         * @return the boxPassword
         */
        public TextBox getBoxPassword() {
                return boxPassword;
        }



        /**
         * @param boxPassword the boxPassword to set
         */
        public void setBoxPassword(PasswordTextBox boxPassword) {
                this.boxPassword = boxPassword;
        }


       
        public String getValeurType(){
                String value = "" ;
                if (getRadioParticulier().getValue()){
                        value = "particulier";
                }else{
                        value = "professionnel" ;
                }
                return value ;
        }
       
        
}
